﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
using System.Configuration;
using System.Data;

namespace ProjectWithAccess
{
    /// <summary>
    /// Summary description for Monopol27
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Monopol27 : System.Web.Services.WebService
    {
        private string ErrorMessage;

        [WebMethod]
        public string getErrorMessage()
        {
            return ErrorMessage;
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        //Funkcija za update na Patuvanje vo bazata
        // pr: patuvanje=true , id=3;
        public void ovozmoziPatuvanje(bool patuvanje, int id)
        {
            try
            {
            string str = "UPDATE TabelaZeleznicka SET Patuvanje = " + patuvanje.ToString() + " WHERE ID=" + id.ToString();

           
                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
            }
        }

        [WebMethod]
        //Metoda koja vraca vrednost za Patuvanje od baza za vneseno ID
        public bool getPatuvanje(int id)
        {
            bool patuvanje;
            try
            {
                string str = "SELECT Momentalna_cena FROM TabelaZeleznicka WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        con.Open();
                        patuvanje = Convert.ToBoolean(cmd.ExecuteScalar());
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
                return false;
            }
            return patuvanje;
        }


         [WebMethod]
        //Funkcija za vracanje na vrednosta Sostojba od baza spored vneseno ID na zeleznicata 
        public string getSostojba(int id)
        {
            string sostojba;
            try
            {
                
                string str = "SELECT Sostojba FROM TabelaZeleznicka WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        con.Open();
                        sostojba = cmd.ExecuteScalar().ToString();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage=e.Message;
                return ErrorMessage;
            }
            return sostojba;
        }

        [WebMethod]
         //Funkcija za promena na vrednost Sostojba vo bazata: (se vnesuva edna od trite vrednosti: hipoteka, kupen, sloboden)
         //Dokolku igracot saka da zeme kredit od bankata, potrebno e da stavi nekoj od imotite pod hipoteka, 
         //dokolku zeleznickata stanica e eden od tie imoti, se povikuva ovaa funkcija
         public void setSostojba(string sostojba, int id)
         {
             if (!sostojba.Equals("hipoteka") && !sostojba.Equals("kupen") && !sostojba.Equals("sloboden"))
             {
                 this.ErrorMessage = "Vnesete edna od vrednostite: kupen (vo sopstvenost na nekoj od igracite), sloboden(vo sopstvenost na bankata) ili hipoteka (pod hipoteka)";
                 throw new System.ArgumentException("Parametarot ne moze da primi vrednost razlicna od: hipoteka,kupen ili sloboden", "sostojba");
             }
             try
             {
                 string str = "UPDATE TabelaZeleznicka SET Sostojba = ?  WHERE ID=" + id.ToString();

                 using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                 {
                     using (OleDbCommand cmd = new OleDbCommand(str, con))
                     {
                         cmd.CommandType = CommandType.Text;
                         cmd.Parameters.AddWithValue("Sostojba", sostojba);
                         con.Open();
                         cmd.ExecuteNonQuery();
                         con.Close();
                     }
                 }

                 //Dokolku sostojba==hipoteka, potrebna e i promena na poleto momentalna_cena, koe treba da dobie 
                 //vrednost ednakva na slednov zbir pocetna cena=200$ => zbir= ((pocetna cena)/2)+(pocetna cena)*10%
                 //ili toa se 220$

                 switch (sostojba)
                 {
                     case "hipoteka": setCena(220, id);
                                      ovozmoziPatuvanje(false, id);
                         break;
                     case "sloboden": setCena(200, id);
                                      setNullForID_sopstvenik(id);
                         break;
                 }

                
             }
             catch (Exception e)
             {
                 this.ErrorMessage = e.Message;
             }
         }

        [WebMethod]
        // Funkcija za update na  Momentalna_cena vo bazata
        public void setCena(double cena, int id)
        {

            try
            {

                string str = "UPDATE TabelaZeleznicka SET Momentalna_cena =? WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("Momentalna_cena", cena.ToString());
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
            }
        }

        [WebMethod]
        //Funkcija za vracanje na vrednosta na cenata od baza spored id 
        public string getCena(int id)
        {
            string sostojba;
            try
            {
                string str = "SELECT Momentalna_cena FROM TabelaZeleznicka WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        con.Open();
                        sostojba = cmd.ExecuteScalar().ToString();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage=e.Message;
                return e.Message;
            }
            return sostojba;
        }

        [WebMethod]
        //Update na vrednosta ID_sopstvenik vo bazata
        public void setID_sopstvenik(int id_sopstvenik, int id)
        {
            try
            {
                string str = "UPDATE TabelaZeleznicka SET ID_sopstvenik = ? WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("ID_sopstvenik", id_sopstvenik.ToString());
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
            }
        }

        [WebMethod]
        //Update na vrednosta ID_sopstvenik vo bazata
        public void setNullForID_sopstvenik(int id)
        {
            try
            {
                string str = "UPDATE TabelaZeleznicka SET ID_sopstvenik = null WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
            }
        }
        
        [WebMethod]
        //Funkcija za vracanje na id-to na sopstvenikot za vneseno id na zeleznicka
        public int getID_sopstvenik(int id)
        {
            int ID_sopstvenik;
            try
            {
                string str = "SELECT ID_sopstvenik FROM TabelaZeleznicka WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        con.Open();
                        ID_sopstvenik = (Int32)cmd.ExecuteScalar();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
                return 0;
            }
            return ID_sopstvenik;
        }

        [WebMethod]
        //Funkcija za kupuvanje na zeleznicata
        public void kupiZeleznicka(int id_igrac, int id)
        {
            try
            {
                setCena(0, id);
                setSostojba("kupen", id);
                setID_sopstvenik(id_igrac, id);
                ovozmoziPatuvanje(false, id);
            }
            catch(Exception e)
            {
                this.ErrorMessage = e.Message;
            }
        }

        [WebMethod]
        //Funkcija za inicijalizacija na momentalniot igrac na zeleznickata
        public void setMomentalenIgrac(int id_igrac,int id_zeleznica)
        {
            try
            {
                string str = "UPDATE TabelaZeleznicka SET Momentalen_igrac = ? WHERE ID=" + id_zeleznica.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("Momentalen_igrac", id_igrac.ToString());
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
            }
          
        }

        [WebMethod]
        //Funkcija za vracanje na id na momentalniot igrac
        //Funkcija za vracanje na id-to na sopstvenikot za vneseno id na zeleznicka
        public int getMomentalenIgrac(int id)
        {
            int IDIgrac;
            try
            {
                string str = "SELECT Momentalen_igrac FROM TabelaZeleznicka WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        con.Open();
                        IDIgrac = (Int32)cmd.ExecuteScalar();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
                return 0;
            }
            return IDIgrac;
        }

        [WebMethod]
        //Update na poleto Destinacija vo baza
        public void setDestinacija(int id, int destinacija)
        {
            try
            {
                string str = "UPDATE TabelaZeleznicka SET Destinacija = ? WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("Momentalen_igrac", destinacija.ToString());
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
            }
          
        }

        [WebMethod]
        //Vracanje na vrednost na poleto Destinacija spored vnesenoto id na zeleznickata
        public int getDestinacija(int id)
        {
            int Destinacija;
            try
            {
                string str = "SELECT Destinacija FROM TabelaZeleznicka WHERE ID=" + id.ToString();

                using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
                {
                    using (OleDbCommand cmd = new OleDbCommand(str, con))
                    {
                        cmd.CommandType = CommandType.Text;
                        con.Open();
                        Destinacija = (Int32)cmd.ExecuteScalar();
                        con.Close();
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
                return 0;
            }
            return Destinacija;
        }

        [WebMethod]
        //Funkcija za insert na nova redica vo bazata koja se povikuva pri kreiranje na sekoja nova instanca od
        // korisnickata kontrola
        public void createNewZeleznica(string ime)
        {
            string str = "INSERT INTO TabelaZeleznicka(Ime,Momentalna_cena,Sostojba,ID_sopstvenik,Patuvanje) VALUES ('" + ime + "','200','sloboden',null,false)";

            using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
            {
                using (OleDbCommand cmd = new OleDbCommand(str, con))
                {
                    cmd.CommandType = CommandType.Text;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }

        }

        [WebMethod]
        //Funkcija za brisenje na redica vo bazata koja se povikuva pri brisenje na korisnicka kontrola
        public void deleteZeleznica(int id)
        {
            string str = "DELETE FROM TabelaZeleznicka WHERE ID =" + id.ToString();

            using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
            {
                using (OleDbCommand cmd = new OleDbCommand(str, con))
                {
                    cmd.CommandType = CommandType.Text;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }

        }

        [WebMethod]
        //Update na imeto na zeleznickata vo baza
        public void setIme(string ime, int id)
        {

            string str = "UPDATE TabelaZeleznicka SET Ime = ? WHERE ID=" + id.ToString();

            using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
            {
                using (OleDbCommand cmd = new OleDbCommand(str, con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("ID_sopstvenik", ime);
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }

        }




    }
}
